#include"comm.hpp"

int main()
{
    key_t k = getKey();
    printf("key: 0x%x\n", k); // key
    int shmid = createShm(k);
    printf("shmid: %d\n", shmid); // shmid
    //sleep(5);//5s之后就会被删除

    char* start = (char*)attchShm(shmid);//挂接成功
    printf("attach success, addresss start: %p\n", start);

    //使用
    while(true)
    {
        //char buffer[]; read(pipefd, buffer)
        printf("client say: %s\n", start);
        struct shmid_ds ds;
        shmctl(shmid, IPC_STAT, &ds);
        printf("获取属性：size: %d, pid: %d, myself: %d, key: 0x%x",\
         ds.shm_segsz,ds.shm_cpid, getpid(), ds.shm_perm.__key);
        sleep(1);
    }

    //去关联：让进程和共享内存丧失关联性
    detachShm(start);
    sleep(5);

    delShm(shmid);
    
    return 0;
}